package com.ruoyi.predict.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.predict.aiDomain.JobRecommendation;
import com.ruoyi.predict.aiDomain.JobRecommendationResponse;
import com.ruoyi.predict.aiDomain.ResultChildVo;
import com.ruoyi.predict.aiDomain.ResultVO;
import com.ruoyi.predict.utils.JsonCleanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.predict.domain.BoleScriptPredictionRecord;
import com.ruoyi.predict.service.IBoleScriptPredictionRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位预测记录Controller
 *
 * @author wwujiale
 * @date 2025-08-03
 */
@RestController
@RequestMapping("/trainScript/predict")
public class BoleScriptPredictionRecordController extends BaseController {
    @Autowired
    private IBoleScriptPredictionRecordService boleScriptPredictionRecordService;

    //查询信息
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        BoleScriptPredictionRecord record = boleScriptPredictionRecordService.selectBoleScriptPredictionRecordByUserId(SecurityUtils.getUserId());
        if (record == null || record.getPredictionResult() == null) {
            return success();
        }

        // 1. 获取原始JSON并清理
        String rawJson = record.getPredictionResult();
//        String cleanedJson = JsonCleanUtils.cleanJsonWithInvalidContent(rawJson);
        ResultVO resultVO = new ResultVO();
        resultVO.setPrediction_time(record.getPredictionTime());
        resultVO.setRemark(record.getRemark());
        try {
            List<JobRecommendation> jobRecommendation = JSON.parseArray(rawJson, JobRecommendation.class);
//        循环赋值
            List<ResultChildVo> resultChildVoList = new ArrayList<>();
            for (JobRecommendation jobRecommendation1 : jobRecommendation) {
                ResultChildVo resultChildVo = new ResultChildVo();
                resultChildVo.setJobName(jobRecommendation1.getJobName());
                resultChildVo.setReason(jobRecommendation1.getReason());
//                resultChildVo.setIndex(jobRecommendation1.getIndex());
                System.out.println(resultChildVo.toString());
                resultChildVoList.add(resultChildVo);
            }

            resultVO.setResult(resultChildVoList);
        }catch (Exception e){
            return success(resultVO);
        }

        // 2. 解析为List<JobRecommendation>

        return success(resultVO);


    }

    /**
     * 查询岗位预测记录列表
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoleScriptPredictionRecord boleScriptPredictionRecord) {
        startPage();
        List<BoleScriptPredictionRecord> list = boleScriptPredictionRecordService.selectBoleScriptPredictionRecordList(boleScriptPredictionRecord);
        return getDataTable(list);
    }

    /**
     * 导出岗位预测记录列表
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:export')")
    @Log(title = "岗位预测记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoleScriptPredictionRecord boleScriptPredictionRecord) {
        List<BoleScriptPredictionRecord> list = boleScriptPredictionRecordService.selectBoleScriptPredictionRecordList(boleScriptPredictionRecord);
        ExcelUtil<BoleScriptPredictionRecord> util = new ExcelUtil<BoleScriptPredictionRecord>(BoleScriptPredictionRecord.class);
        util.exportExcel(response, list, "岗位预测记录数据");
    }

    /**
     * 获取岗位预测记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(boleScriptPredictionRecordService.selectBoleScriptPredictionRecordById(id));
    }

    /**
     * 新增岗位预测记录
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:add')")
    @Log(title = "岗位预测记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoleScriptPredictionRecord boleScriptPredictionRecord) {
        return toAjax(boleScriptPredictionRecordService.insertBoleScriptPredictionRecord(boleScriptPredictionRecord));
    }

    /**
     * 修改岗位预测记录
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:edit')")
    @Log(title = "岗位预测记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoleScriptPredictionRecord boleScriptPredictionRecord) {
        return toAjax(boleScriptPredictionRecordService.updateBoleScriptPredictionRecord(boleScriptPredictionRecord));
    }

    /**
     * 删除岗位预测记录
     */
    @PreAuthorize("@ss.hasPermi('trainScript:predict:remove')")
    @Log(title = "岗位预测记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(boleScriptPredictionRecordService.deleteBoleScriptPredictionRecordByIds(ids));
    }
}
